<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        /* 
            数组常用函数的讲解：
                1. at() 方法接收一个整数值并返回该索引的项目
                2. indexOf 方法
         */
        /* const array1 = [5,12,8,130,44];
        
        let index = 2;
        //根据索引返回对应的值
        console.log(`An index of ${index} returns ${array1.at(index)}`);
        // Expected output: "An index of 2 returns 8"

        index = -2;

        console.log(`An index of ${index} returns ${array1.at(index)}`); */
        // Expected output: "An index of -2 returns 130"

        // index = -1;
        // console.log(array1.at(index));

        // //如果对应的index不存在呢？
        // index = 100;
        // console.log(array1.at(index));  //undefined


        // const beasts = ['aa', 'bb', 'cc', 'dd', 'ee','aa','cc'];
        // const beasts1 = ['aa', 'bb', 'dd', 'dd', 'ee','dd','cc'];
        // 返回指定的值对应的索引
        // console.log(beasts.indexOf('aac'));  //0
        // console.log(beasts.indexOf('cc', 3));   //从指定的索引位置开始找指定的值对应的索引 从左往右找
        // /* 
        //     -1 : 从最后一个元素开始。
        //     -3 ：从倒数第三个元素开始
        // 负数仍是从当前索引位置开始向右找
        //  */
        // console.log(beasts1.indexOf('dd', -3));
        
        
        /* 
            lastIndexOf：返回元素最后一次出现的索引
         */
         const beasts1 = ['aa', 'bb', 'dd', 'dd', 'ee','dd','cc'];

        console.log(beasts1.lastIndexOf('dd1'));

        var array = [2, 5, 9, 2];
        var index = array.lastIndexOf(2);   // index is 3 从指定索引位置向前找 从右往左找
        console.log(index);
        index = array.lastIndexOf(7);
        console.log(index);             // index is -1
        index = array.lastIndexOf(2, 3);    // index is 3
        console.log(index);
        index = array.lastIndexOf(2, 2);    
        console.log(index);             // index is 0
        index = array.lastIndexOf(2, -2);
        console.log(index); // index is 0
        index = array.lastIndexOf(2, -1); // index is 3  

    </script>
</head>
<body>
    
</body>
</html>